//
//  ViewController.m
//  16.Accelerometer
//
//  Created by iD Student on 7/8/13.
//  Copyright (c) 2013 iD Student. All rights reserved.
//
#define paused 0
#define running 1

#import "ViewController.h"

@interface ViewController ()

@end
@implementation ViewController

- (void)viewDidLoad
{
    ball = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ball.png"]];
    ball.frame = CGRectMake(self.view.center.x - 17.5, self.view.center.y + 95, 35, 35);
    [self.view addSubview:ball];

    x = 0;
    y = 0;
    gameState = paused;
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    row = [[NSMutableArray alloc]init];
    column = [[NSMutableArray alloc]init];
    for (int t = 0; t < 6; t++) {
        
        for (int i = 0; i < 8; i++) {
            UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(x,y, 46, 30)];
           /* NSLog([NSString stringWithFormat:@"X:%i Y:%i Width:%f Height:%f",x,y,image.frame.size.width, image.frame.size.height]);*/
            image.image = [UIImage imageNamed:@"block.png"];
            x += image.frame.size.width;
            [row addObject:image];
            [self.view addSubview:image];
        }
        [column addObject:row];
        row = [[NSMutableArray alloc]init];
        y += 30;
        x = 0;
        
      
    }
}


- (void) setXSpeed;
{
    [ball setCenter:CGPointMake(ball.center.x + xSpeed, ball.center + ySpeed)];
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(gameState == paused)
    {
        xSpeed = arc4random() % 30 - 15;
        ySpeed = 5;
        timer = [NSTimer timerWithTimeInterval:.5 target:self selector:@selector(setXSpeed) userInfo:nil repeats:YES];
        
        gameState = running;
    }
    else {
        UITouch *touch = [touches anyObject];
        paddle.center = CGPointMake([touch locationInView:self.view].x, paddle.center.y);
        if (paddle.center.x < paddle.frame.size.width/2) {
            paddle.center = CGPointMake( paddle.frame.size.width/2, paddle.center.y);
        }
        else if (paddle.center.x > 640 - paddle.frame.size.width/2) {
            paddle.center = CGPointMake(640 - paddle.frame.size.width/2, paddle.center.y);
        }
    }
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (gameState == paused) {
        xSpeed = arc4random() % 30 - 15;
        ySpeed = 5;
        gameState = running;
    }
    else {
        UITouch *touch = [touches anyObject];
        paddle.center = CGPointMake([touch locationInView:self.view].x, paddle.center.y);
        if (paddle.center.x < paddle.frame.size.width/2) {
            paddle.center = CGPointMake(paddle.frame.size.width/2, paddle.center.y);
        }
        else if (paddle.center.x > 320 - paddle.frame.size.width/2)
            paddle.center = CGPointMake(320 - paddle.frame.size.width/2, paddle.center.y);
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
